home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr48 / 386p_200.zip / RM_SOUND.TXT < prev    next >
Text File  |  1995-01-15  |  14KB  |  289 lines

  1. HOW YOU CAN PRODUCE DIGITAL SOUND
  2. Some preliminary explanations you should read before looking into xsd.txt ....
  3. ... and some detailate descriptions of the internal working of 386Mixer.
  4.  
  5. If you think graphics routine are complex, wait to see how sound
  6. generation works.
  7.  
  8. First of all, "sound" is the input we collect with our ears.
  9. The thing we call sound is actually a sum of waves of pressure
  10. propagating thru solid,liquid and gas and "bouncing" into our ears.
  11.  
  12. Usually you can think at "plain" waves as made by a ponderated sum
  13. of sinusoidal waves of various frequencies (frequency is the reciprocal
  14. of the sinusoidal wave period and it is measured in Hertz)
  15. (Hertz = 1/sec).
  16.  
  17. Our ears can detect sinusoidal waves with a frequency between 50..20000 Hertz
  18. (well, some people has a wider range).
  19. Below 50hz we have infrasounds (that we don't hear but that can cause
  20. us various troubles if too much poweful)(remember a wave carries energy)
  21. Above 20000 Hertz we have ultrasounds
  22. [we don't hear 'em because they are outside the resonance frequency range
  23.  of our "sound to neural" converter system (it is a mechanical
  24.  amplifier coupled to a "flash adc" made of resonating cell devices) (cool!)
  25.  and they too can cause us troubles if too much loud].
  26.  
  27. Because sounds are just waves, they have all the nice attributes of waves.
  28. Do you remember all those nasty theorems about information transmission ?
  29. Well! now you can hear :) how they are useful:
  30. If you modulate a sinusoidal sound generator cycling at twice the maximum
  31. hearable frequency you can replicate "perfectly" (for a human) a sound.
  32. This explains why the base frequency of choice for high quality digital sound
  33. is around 44000Hz (a little above twice 20000Hz).
  34. The "modulation frequency" is usually called base frequency
  35. or playback frequency
  36. (because from it you can "simulate" the lower frequency waves).
  37.  
  38. Going below 44000Hz you produce  "not so good" sound, but depending on the
  39. application you can get very low, for example "telephone line" playback
  40. quality is around 8000Hz and radio station quality is around 11000...22000Hz
  41. (depending if AM or FM ).
  42.  
  43. Then comes how smoothly you amplitude-modulate the base frequency
  44. 8bit sample resolution on 8000Hz is digital phone line quality
  45. 16bit sample resolution on 44000Hz is compact disk quality.
  46. Notice i don't talk of sound/noise ratio, this depends on the hardware
  47. you use and on the "sound synthesis" method you use.
  48.  
  49. Digital sound playback adds some "noise" because of time and amplitude
  50. quantization, so the higher the sample resolution and the playback frequency
  51. the better, but this raises the i/o bandwidth utilized by the sound system.
  52. With 8bit at 8000Hz the sound system manages around 8Kbyte/sec
  53. but this if you play only one sound at a time (one voice), if say you
  54. want to make 20 sounds playing at the same time you have to add 'em
  55. together before sending 'em to the sound reproduction system
  56. and this means having to manage 20*8= 160Kbyte/sec ...
  57. .... and if you want 20 voice CD quality, you have to manage around
  58. 44*2*20 = 1.760 Mbyte/sec.
  59. This explains why high-quality soundcards usually comes with DMA support
  60. and on-board processing power to reduce the cpu work.
  61.  
  62. Ok! I know the basics, but what i have to do to make sound?
  63.  
  64. First of all you need a DAC (Digital to Analog Converter):
  65.              It is a circuit that takes in input a digital value
  66.              anc converts it to an analog value
  67.              (usually a voltage level).
  68.              If you properly connect a DAC to a speaker
  69.              you can make sound by sending sequences of digital values to it.
  70.  
  71. PC BEEPER:
  72.  
  73. The original PC has a "poor man" DAC made by a 1bit TTL output
  74. connected to the PC speaker [ well, if you dare to call "airplane"
  75. a cow with a propeller hat, you can call the pc-beeper system a 1bit DAC :) ].
  76.  
  77. It was originally intended to make "buzz" and "beeps" like old terminals.
  78. To make these things easier, it is connected to the channel 2 of the
  79. Intel 8253 PIT (Programmable Interval Timer).
  80. The PIT is the "timer chip" every PC has, this can "count"
  81. a variable time interval and generate various "count related" outputs.
  82. The 8253 has three indipendent counters and outputs (time channels)
  83. [ The "system timer" 18,2Hz  IRQ 0 (INT 8) is the PIT channel 0 output
  84.   channel 1 was used for ram refresh on older systems
  85.   and channel 2 output can be connected to the speaker
  86.   by flipping a bit in the keyboard controller output port]
  87.  
  88. What's more, the PIT can GENERATE SQUARE WAVES with a frequency range
  89. between 18,2Hz to 1193180Hz.
  90. So if you connect the PIT channel 2 output to the speaker
  91. and program channel 2 to emit a square wave with a frequency higher
  92. than 50Hz you can hear a constant sound coming from the speaker.
  93.  
  94. This is good to make "buzzs and beeps" (as it was originally intended)
  95. but it is not very good to make sound (as it was NOT intended).
  96.  
  97. A square wave is nearly the worst thing you can use for
  98. "amplitude modulation" sound synthesis (a "square" wave is equivalent
  99. to a big pack of sinusoidal waves), this means that if you use 1bit square wave
  100. modulation you ADD LOTS OF NOISE to digital sound sample you
  101. want to playback because you dont' modulate a single sinusoidal wave
  102. you actually modulate LOTS of different sinusoidal waves!
  103. (this explains why digital sound on the pc-speaker is so "noisy").
  104.  
  105. You can partially reduce noise AND raise digital samples
  106. resolution this is you think that
  107. the pc-speaker output is connected to a speaker circuit
  108. that is equivalent to a "poor man" low-pass (RC) filter
  109. (actually, it's more like an RLC, but what the heck!).
  110.  
  111. "Pulsing" trains of square waves in a frequency range that goes
  112. between "speaker membrane fully oscillating" to "speaker membrane not oscillating"
  113. you can have smoother modulation control.
  114. You can perform sort of "pulse width modulation" sound synthesis
  115. with a NEARLY 6bit resolution
  116. (the "low-pass smoothed" square wave will look like a
  117.  smells-like-a-sinusoid wave, with amplitude depending on how "large"
  118.  was the square wave).
  119. (see the pc-speaker code file i included for more info).
  120.  
  121. DACs ON THE PRINTER PORT:
  122.  
  123. Well, what if you want to use something better and are short of cash?
  124. YOU MAKE DO A DAC OUTPUT ON YOUR OWN!!!!
  125. Now put on your propeller hat, look in a digital elctronics book
  126. about DACs and you 'll discover the uncanny truth:
  127.  DACs ARE EASY TO BUILD ON YOUR OWN!!!!
  128.  If you don't believe me, look into jdac.txt
  129.  it includes all the info you need to make a dac to plug into the
  130.  printer port.
  131.  
  132. If you can't buy a soundcard but have enough resistors
  133. and capacitors, you can make a simple 8bit "scaled resistors" dac
  134. and plug it into the printer port (it has an 8bit digital output).
  135.  
  136. There are lots of "sound device on printer port" devices around
  137. (the SoundPlayer, various COVOX devices, the Disney SoundSource
  138.  and lots of other things) but they cost more than their value.
  139. If you give a look at jdac.txt you will discover that an equivalent thing
  140. can be build spending less than 10$ (or nothing, a friend of mine simply
  141. "cannibalized" old radios and other electronic things and reutilized
  142. resistors and capacitors)
  143. [ well, the same guy made a 100Watt amplifier "reutilizing" things
  144.  ("big guts" RMS watts, not those idiot-fooling peak-to-peak VA ratings
  145.    you usually find stamped in big letters on stereo packages)
  146.  but the recycled dac is really easy to make].
  147.  
  148. [ By the way, recycling electronic devices is "ecologically correct"
  149.   costs you nothing (once you have the "basic tools" and data books)
  150.   and you can put your hands on "big gun" electronics you usually
  151.   don't find in the electronic stores (like poweful electric engines,big
  152.   power converters, laser devices, microwave generators and other things
  153.   like that) ]  (look into tronics.txt for more info about "recycled" items).
  154.  
  155. THE PROBLEM WITH "simple" DACS:
  156.  
  157. But to playback the samples you have to change the dac output
  158. at a constant rate (equivalent to the "playback frequency").
  159. On a "just the dac" system you have to reprogram
  160. the timer interrupt frequency (PIT CHANNEL 0)
  161. to make it play at at least 8000Hz, and then you have to hook that
  162. irq so the interrupt handler will change the dac output.
  163.  
  164. Such high interrupt rate spells disaster because processing an irq request
  165. takes some time, time you steal to the execution of your "main" programs!
  166. Usually on a 386, 16000hz is the maximum pc-speaker playback rate
  167. you can get (with your main program slower than an 8086 turtle).
  168.  
  169. So it's time to call in a good soundcard for sound support.
  170.  
  171. MORE THAN "just a dac", THE SOUNDBLASTER:
  172.  
  173. A SoundBlaster (SB for short) or equivalent card has an
  174. on-board 8bit DAC (the SB16 has a 16bit DAC)
  175. PLUS a very weak DSP chip that helps reduce the cpu burden
  176. [Yeah! and my Fiat Uno car is a very weak Ferrari! ;) ].
  177.  
  178. What's more it has DMA support.
  179. Once you program the SB and DMA hardware the SB card can "self-time"
  180. and fetch sound samples from the pc memory using the DMA system.
  181.  
  182. The SB DAC uses PCM (pulse code modulation) on 8bit cards
  183. (a sample gets converted to a "voltage pulse" that drops to zero
  184.  if you don't send more samples)
  185. and SAM (signal amplitude modulation) on 16bit cards (plus 8bit PCM support)
  186. (with SAM a sample gets converted to a "voltage level" that does not
  187.  change if you don't send other samples).
  188.  
  189. If the board has stereo output support (like the SPpro and successive)
  190. it can "distribute" output on left and right speaker outputs
  191. (the first sample goes to the left speaker, the second
  192.  to the right speaker, the third to  the left, the fourth to the right
  193.  and so on).
  194. This explains why on a SBpro you can have max. 44000Hz MONO output
  195. OR max. 22000Hz STEREO output.
  196.  
  197. An SB or equivalent card has just ONE DIGITAL VOICE
  198. (can play only one stream of digital sound samples)
  199. but "plays itself" up to 64kbytes of digital sound (this is a dma limit)
  200. and by way of an interrupt handler managing the SB irq line
  201. it can be "cpu reprogrammed" on the flight
  202. so when a block of data ends plying, another block of data can be started.
  203.  
  204. Told in another way: Once you program the dma chips and the SB chip
  205. you can make "automatically play" up to 64k of sound data without
  206. and at the same time the CPU can make other things.
  207.  
  208. MULTIPLE VOICES ON A SINGLE VOICE DEVICE:
  209.  
  210. So, what to do if you want multiple voices on a SB card?
  211. You simply "add 'em together" in a single sound sample block and then
  212. send it to the SB card by way of DMA.
  213.  
  214. This of course can mean lots of cpu work again, but it requires less
  215. works than with a "just the dac" device, because
  216. you spend time mixing the sound data, but you don't spent time
  217. sending it to the output dac (the souncard driven dma system
  218. does it all).
  219.  
  220. "HARDWARE BASED" MULTIPLE VOICE DIGITAL OUTPUT:
  221.  
  222. So, what to do if you want "something better" ?
  223. Let's think again, a SB card is a SINGLE VOICE WAVETABLE DEVICE
  224. (also known as a DAC plus a few support chips :} ).
  225. A wavetable device is:
  226.          a DAC plus "data feed" circuits that plays a sound using a
  227.          sequence of sound samples in "table" stored into ram
  228.          with programable sound volume and playback rate.
  229.  
  230. What about a MULTIPLE VOICE WAVETABLE DEVICE?
  231. Immagine a soundcard with multiple programmable wavetable devices ...
  232. ... like a Gravis UltraSound for example!
  233. (Or the SB32AWE, or the Ensoniq or ...)
  234. Those cards have a multiple voice wavetable system, but they cannot fetch
  235. their sound samples from system ram (there are very few DMA channels
  236. available and a single 16bit ISA DMA channel tops at 2Mbyte/sec), so they
  237. need on-board ram to store wavetables to play.
  238. With such a device you can play multiple digital voices
  239. with nearly zero cpu overhead.
  240.  
  241. No more problems, now? Not actually, what if you have too few ram
  242. and too many wavetables to play?  Well, you can use the DMA system
  243. "a la SB" and fetch data from memory for one digital voice, if you need
  244. more voices from system ram, you can mix them like with the SoundBlaster.
  245.  
  246. Another thing you can find with "wavetable hardware" is panning control
  247. (panning is the "direction" from where the sound is coming if you immagine it
  248.  between the left and right speaker) and this can help you too
  249.  if you want "stereo sound source positioning".
  250.  
  251. EFFECTS NOT SUPPORTED DIRECTLY:
  252.  
  253. You may think this is enough for most of the sound effects you want, isn't it?
  254. Well, some nice things are still missing, but they can be added
  255. by way of clever irq handlers that reprogram the sound hardware
  256. "on the flight".
  257.  
  258. Doppler effect: the alteration of the wave frequencies caused by the
  259. motion and/or acceleration of the sound source.
  260.  
  261. Echo and surround: "reflected sound" effects.
  262.  
  263. 3D sound: a complex "differentiated-controlled echo" simulating
  264. the sound distortion caused by ears' padillions. We detect the tridimensional
  265. position of the sound source by way of this distortion.
  266. To simulate 3d sound you need multiple voice playing the same mono sample
  267. with different panning,volume and delays.
  268.  
  269. I've included "hooks" for PLAYBACK RATE RAMPING in the mixer and at
  270. device driver level.
  271.  
  272. Once you have ALL the other effects AND playback rate ramping
  273. you can perform lots of effects
  274. including plain doppler effect and other things like the doppler distortion
  275. caused by sound sources moving/accelerating at high speed around you.
  276. (if you want to make a big-gun-mecha simulator including
  277.  high-speed close-range combat this is a thing you HAVE to add)
  278. [ this way the player will be able to "hear"
  279.   if a mecha BEHIND him is changing speed or getting closer or
  280.   more far !!!!] [Immagine this "audiovisual" support in a mecha simulator
  281.   where you can fight with fists and kicks too!!]
  282.   [ So for example you can "hear what's going on behind you"
  283.     and so you can "sweep" 180 degrees and slash with a razor kick
  284.     (have i told you your mecha has razor boots?)
  285.     the mecha attacking you from behind ]
  286. (if you have enough cpu power)
  287. (maybe you will hear :) something about this in 1997)
  288.  
  289.